<!--
 * @Author: wangding
 * @Date: 2020-11-09 14:33:19
 * @LastEditTime: 2021-01-04 14:13:27
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \solaxcloud\src\views\device\sn\sn.vue
-->
<template>
    <div class="found-sn-main-box">
        <div class="search-box">
            <div class="search-box-sub">
                <span class="lable-text">{{$t('Version')}}</span>
                <el-input v-model="searchData.fullVersion" clearable></el-input>
            </div>
            <!-- <div class="search-box-sub">
                <span class="lable-text">{{$t('version.versionAdd.auditStatus')}}</span>
                <el-select v-model="searchData.checkFlag" clearable>
                    <el-option v-for="item in checkList" :key="item.value" :label="item.name" :value="item.value"></el-option>
                </el-select>
            </div> -->
            <div class="search-box-sub">
                <span class="lable-text">{{$t('version.versionService.publishStatus')}}</span>
                <el-select v-model="searchData.status" clearable>
                    <el-option v-for="item in versionStatusList" :key="item.value" :label="item.name" :value="item.value"></el-option>
                </el-select>
            </div>
            
            <div class="search-box-sub">
                <el-button type="primary" @click='query'>{{$t('query')}}</el-button>
            </div>
        </div>
        <div class="button-box">
            <el-button type="primary"  icon='el-icon-plus' @click='addModal'>{{$t('add')}}</el-button>
        </div>
        <baseTable v-loading="loading"  row-key='sid' :data="dataRows" border  stripe   :column='column' :select='false'>
            <template #checkFlag="{data}">
                {{comm.returnCode(checkList,data.checkFlag,'value','name')}}
            </template>
            <template #status="{data}">
                {{returnStatus(versionStatusList,data)}}
            </template>
            <template #option="{data}">
                <div class="option-box">
                    <el-popconfirm
                        :confirm-button-text="$t('sure')"
                        :cancel-button-text="$t('cancel')"
                        :title="$t('device-confirm-verify')"
                        @confirm='devgatewayupdateUpdate({id:data.sid,checkFlag:1})'
                        v-if="data.checkFlag==0"
                    >
                        <el-button class="marginR10" slot="reference" icon='iconfont icon-yanjing' :title="$t('version.versionService.check')"></el-button>
                    </el-popconfirm>

                    <el-popconfirm
                        :confirm-button-text="$t('sure')"
                        :cancel-button-text="$t('cancel')"
                        :title="$t('device-re-verify')"
                        @confirm='devgatewayupdateUpdate({id:data.sid,checkFlag:0})'
                        v-if="data.checkFlag==1"
                    >
                        <el-button class="marginR10" slot="reference" icon='iconfont icon-jurassic_loseeyes' :title="$t('version.versionAdd.Retrial')" style="color:red;"></el-button>
                    </el-popconfirm>

                    <el-popconfirm
                        :confirm-button-text="$t('sure')"
                        :cancel-button-text="$t('cancel')"
                        :title="$t('version.versionService.publishSure')"
                        @confirm='devgatewayupdateUpdate({id:data.sid,status:1})'
                        v-if="data.status!=1"
                    >
                        <el-button class="marginR10" slot="reference" icon='el-icon-s-promotion' :title="$t('version.versionService.publish')"></el-button>
                    </el-popconfirm>
                    
                    <el-popconfirm
                        :confirm-button-text="$t('sure')"
                        :cancel-button-text="$t('cancel')"
                        :title="$t('version.versionService.forbiddenSure')"
                        @confirm='devgatewayupdateUpdate({id:data.sid,status:0})'
                        v-if="data.status!=0"
                    >
                        <el-button class="marginR10" slot="reference" icon='iconfont icon-jinyong' :title="$t('version.versionService.forbidden')"></el-button>
                    </el-popconfirm>
                </div>
            </template>
        </baseTable>
        <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="current"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
        </el-pagination>
        <add @uploadData='query' v-if='newVisible'></add>
    </div>
</template>

<script>
import baseTable from '../../../components/common/baseTable'
import add from './add'
export default {
    components: {
        baseTable,
        add
    },
    props: {

    },
    data() {
        return {
            searchData:{
                fullVersion:'',
                checkFlag:'',
                status:''
            },
            checkList:[{name:this.$t('Unaudited'),value:'0'},{name:this.$t('Audited'),value:'1'}],
            versionStatusList:[{name:this.$t('Unpublished'),value:'0'},{name:this.$t('Audited'),value:'1'},{name:this.$t('Testing'),value:'2'}],
            loading:false,
            dataRows:[],
            size:10,
            total:1,
            current:1,
            column:[{
                title: this.$t('全版本号'),
                value: 'fullVersion'
            },{
                title: this.$t('uploadtime'),
                value: 'uploadTime',
            },{
                title: this.$t('version.versionAdd.auditStatus'),
                value: 'checkFlag',
                slot:'checkFlag'
            }, {
                title: this.$t('version.versionService.publishStatus'),
                value: 'status',
                slot:'status'
            },{
                title: this.$t('control'),
                slot: 'option',
            }]
        };
    },
    computed: {
        newVisible(){
            return this.$store.state.sysStore.newVisible
        }
    },
    watch: {

    },
    created() {

    },
    mounted() {
        this.initTable()
    },
    methods: {
        addModal(){
            this.$store.commit('setNewVisible',true)
        },
        async deviceSwitch(id){
            let res = await this.API.devgatewayDeviceSwitch({
                gatewayIds:id,
                state:1
            })
            if(res.success){
                this.$message.success(this.$t('index.control_success'))
            }else{
                this.$message.error(this.$t('index.control_fail'))
            }
            this.initTable()
        },
        async resetCipher(id){
            let res = await this.API.devgatewayResetCipher({
                gatewayIds:id
            })
            if(res.success){
                this.$message.success(this.$t('index.control_success'))
            }else{
                this.$message.error(this.$t('index.control_fail'))
            }
            this.initTable()
        },
        async updateDevice(id){
            let res = await this.API.devgatewayupdategetList()
            console.log(res,'res')
        },
        async initTable(){
            this.loading = true
            let checkFlag=''
            if(this.searchData.status=='1'){
                checkFlag='1'
            }else if(this.searchData.status=='2'){
                checkFlag='0'
            }
            let res = await this.API.devgatewayupdateGetPage({
                size:this.size,
                current:this.current,
                fullVersion:this.searchData.fullVersion,
                checkFlag:checkFlag,
                status:this.searchData.status==2?'1':this.searchData.status
            })
            this.total = res.total
            this.dataRows = res.rows
            this.loading = false
        },
        async devgatewayupdateUpdate(params){
            this.loading = true
            let res = await this.API.devgatewayupdateUpdate(params)
            this.loading = false
            if(this.comm.showMsg(res.success)){
                this.initTable()
            }
        },
        returnStatus(dataList,data){
            if(data.status=='0'){
                return this.$t('Unpublished')
            }else if(data.status=='1'){
                if(data.checkFlag=='0'){
                    return this.$t('Testing')
                }else{
                    return this.$t('Published')
                }
            }
        },
        query(){
            this.current = 1
            this.initTable()
        },
        handleCurrentChange(val) {
          this.current = val
          this.initTable()
        },
        handleSizeChange(val) {
          this.size = val
          this.current = 1
          this.initTable()
        },
    },
};
</script>

<style  lang="less" scoped>
.found-sn-main-box{
    
}
</style>
